package com.shenyi.shop.service;

import com.shenyi.core.utils.BeanUtils;
import com.shenyi.core.utils.StringUtils;
import com.shenyi.shop.api.IProductService;
import com.shenyi.shop.dao.GoodsSpecificationMapper;
import com.shenyi.shop.dao.ProductMapper;
import com.shenyi.shop.entity.GoodsSpecificationDO;
import com.shenyi.shop.entity.ProductDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/05
 * @desc
 * @see
 * @since 1.0
 */
@Service
public class ProductServiceImpl implements IProductService {


    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private GoodsSpecificationMapper goodsSpecificationMapper;

    @Override
    public ProductDO queryObject(Integer id) {
        return productMapper.queryObject(id);
    }

    @Override
    public List<ProductDO> queryList(Map<String, Object> map) {
        List<ProductDO> list = productMapper.queryList(map);

        List<ProductDO> result = new ArrayList<>();
        //翻译产品规格
        if (null != list && list.size() > 0) {
            for (ProductDO item : list) {
                String specificationIds = item.getGoodsSpecificationIds();
                String specificationValue = "";
                if (!StringUtils.isNullOrEmpty(specificationIds)) {
                    String[] arr = specificationIds.split("_");

                    for (String goodsSpecificationId : arr) {
                        GoodsSpecificationDO entity = goodsSpecificationMapper.queryObject(goodsSpecificationId);
                        if (null != entity) {
                            specificationValue += entity.getValue() + "；";
                        }
                    }
                }
                item.setSpecificationValue(item.getGoodsName() + " " + specificationValue);
                result.add(item);
            }
        }
        return result;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return productMapper.queryTotal(map);
    }

    @Override
    @Transactional
    public int save(ProductDO product) {
        int result = 0;
        String goodsSpecificationIds = product.getGoodsSpecificationIds();
        if (!StringUtils.isNullOrEmpty(goodsSpecificationIds)) {
            String[] goodsSpecificationIdArr = goodsSpecificationIds.split("_");
            for (int i = 0; i < goodsSpecificationIdArr.length - 1; i++) {
                String[] oneId = goodsSpecificationIdArr[i].split(",");
                String[] twoId = goodsSpecificationIdArr[i + 1].split(",");
                for (int j = 0; j < oneId.length; j++) {
                    for (int k = 0; k < twoId.length; k++) {
                        String strGoodsSpecificationIds = null;
                        if (StringUtils.isNullOrEmpty(oneId[j]) || StringUtils.isNullOrEmpty(twoId[k])){
                            continue;
                        }
                        strGoodsSpecificationIds = oneId[j] + "_" + twoId[k];
                        product.setGoodsSpecificationIds(strGoodsSpecificationIds);
                        ProductDO entity = new ProductDO();
                        BeanUtils.copyProperties(product, entity);
                        result += productMapper.save(entity);
                    }
                }
            }
        }
        return result;
    }

    @Override
    public int update(ProductDO product) {
        if (StringUtils.isNullOrEmpty(product.getGoodsSpecificationIds())){
            product.setGoodsSpecificationIds("");
        }
        return productMapper.update(product);
    }

    @Override
    public int delete(Integer id) {
        return productMapper.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return productMapper.deleteBatch(ids);
    }
}
